﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint.Administration;
using Microsoft.Build.Framework;
using System.Collections;
using Microsoft.Build.Utilities;

namespace SPALM
{
    public partial class SiteCollectionSelector : Form
    {
        public ArrayList taskItems { get; set; }

        public SiteCollectionSelector()
        {
            InitializeComponent();
            taskItems = new ArrayList();
        }

        public SiteCollectionSelector(string title, string messageText)
        {
            this.InitializeComponent();
            taskItems = new ArrayList();

            this.labelText.Text = messageText;
            this.Text = title;
            this.wizardPicture1.Headline = title;

            LoadWebApplications();
        }


        BackgroundWorker worker = null;
        private void LoadWebApplications()
        {
            this.Cursor = Cursors.WaitCursor;

            treeView1.Nodes.Clear();

            worker = new BackgroundWorker();
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerAsync();
        }

        private delegate void AddRootItemCallback(string webappurl, string displayName);
        private void AddRootItem(string webappurl, string displayName)
        {
            if (treeView1.InvokeRequired)
            {
                AddRootItemCallback d = AddRootItem;
                Invoke(d, new object[] { webappurl, displayName });
            }
            else
            {
                treeView1.Nodes.Add(webappurl, displayName).Tag = webappurl;
            }
        }

        private delegate void AddChildItemCallback(string webappurl, string siteurl, string sitetitle);
        private void AddChildNodeItem(string webappurl, string siteurl, string sitetitle)
        {
            if (treeView1.InvokeRequired)
            {
                AddChildItemCallback d = AddChildNodeItem;
                Invoke(d, new object[] { webappurl, siteurl, sitetitle });
            }
            else
            {
                try
                {
                    TreeNode childNode = treeView1.Nodes[webappurl].Nodes.Add(siteurl, sitetitle);
                    childNode.Tag = siteurl;

                }
                catch { }
            }
        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            foreach (SPWebApplication webapp in SPWebService.ContentService.WebApplications)
            {
                string url = "";
                foreach (SPAlternateUrl alterurl in webapp.AlternateUrls)
                {
                    url = alterurl.IncomingUrl;
                    break;
                }
                AddWebApp(webapp, url);
            }
        }

        private void AddWebApp(SPWebApplication webapp, string url)
        {
            AddRootItem(url, webapp.Name);
            foreach (Microsoft.SharePoint.SPSite site in webapp.Sites)
            {
                AddChildNodeItem(url, site.Url, site.RootWeb.Title);
            }
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.button1.Enabled = true;
            this.Cursor = Cursors.Default;

            if (e.Error != null)
            {
                //Exception ocurred
                textBox_Status.Text = e.Error.ToString();
                panel_Status.Visible = true;
            }
            else
            {
                panel_Status.Visible = false;
                this.treeView1.Visible = true;
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            UpdateSelection();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void WebApplicationSelector_Load(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Level == 0)
            {
                foreach (TreeNode childNode in e.Node.Nodes)
                {
                    childNode.Checked = e.Node.Checked;
                }
                e.Node.Expand();
            }
            UpdateSelection();
        }

        private void UpdateSelection()
        {
            taskItems.Clear();

            foreach (TreeNode webAppnode in treeView1.Nodes)
            {
                foreach (TreeNode siteCollNode in webAppnode.Nodes)
                {
                    if (siteCollNode.Checked)
                    {
                        TaskItem item = new TaskItem(siteCollNode.Tag.ToString());
                        item.SetMetadata("Value", siteCollNode.Tag.ToString());
                        item.SetMetadata("Type", "SiteCollection");
                        taskItems.Add(item);
                    }
                }
            }

            string displayValue = "";
            foreach (TaskItem item in taskItems)
            {
                if (displayValue != "")
                {
                    displayValue += ";";
                }
                displayValue += item.GetMetadata("Value");
            }
            textBox1.Text = displayValue;
        }
    }
}
